﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.WeixinMP.Impls;
using Microhard.WeixinMP.Interfaces;
using Microhard.WeixinMP.Unities;
using Microhard.WeixinMP.Models;
using System.Configuration;
using Micrhard.FMCG.Server.Msg;
using Microhard.Permission.Interface;
using Microhard.Core;
using Micrhard.FMCG.Server.Interface;

namespace Micrhard.FMCG.Server.Weixin
{
    public class WriteMyDailyReport : BaseResponseImpl
    {
        public override Microhard.WeixinMP.Interfaces.WeixinCommand Command
        {
            get
            {
                WeixinCommand wc = new WeixinCommand();
                wc.Event = "CLICK";
                wc.EventKey = "myDaily";
                return wc;
            }
        }

        protected override Microhard.WeixinMP.Models.WeixinBaseData<object> CreateFeatureResponse(Microhard.WeixinMP.Models.RequestServiceModel request, Microhard.WeixinMP.Models.WeixinBaseData<object> response)
        {
            //创建token
            string token = SessionSecurity.CreateToken(response.ToUserName);
            //创建会话成功，发起服务
            response.MsgType = WeixinMsgType.Text;
            response.FuncFlag = WeixinMsgType.FuncFlag_False;
            WeixinTxt result = new WeixinTxt();

            //验证用户是否合法
            IWeiXinAccount iwa = IoC.Resolve<IWeiXinAccount>();
            string realUserName = string.Empty;
            bool illegal = iwa.ValidateByWeiXinUserName(response.ToUserName, out realUserName);
            if (illegal)
            {
                IDaily daily = IoC.Resolve<IDaily>();
                bool hasWrite = daily.IsDuplicate(new Models.DailyModel { CurrentLoginUserName = realUserName });
                var now = DateTime.Now;
                if (hasWrite)
                {
                    result.Content = string.Format(DailyMsg.HasWrite, now.Date.ToString("yyyy-MM-dd"));
                }
                else
                {
                    //生成链接地址
                    string askQuestionUrl = ConfigurationManager.AppSettings["MyDailyUrl"];
                    string finUrl = string.Format(this.UrlFormat, askQuestionUrl, token, response.EncodeWeixinToUserName, string.Empty, request.ConversationID);
                    result.Content = string.Format(DailyMsg.DailyUrlLink, finUrl, now.Date.ToString("yyyy-MM-dd"));
                }
            }
            else
            {
                result.Content = NoticeMsg.NoneUser;
            }
            response.Content = result;
            return response;
        }

    }
}
